package com.example.provider.service.Impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.common.model.ArticleArticleParticipants;
import com.example.common.model.UserAttention;
import com.example.common.model.condition.ArticleArticleParticipantsCondition;
import com.example.provider.annotation.ThreadPoolStatus;
import com.example.provider.mapper.ArticleArticleParticipantsMapper;
import com.example.provider.service.ArticleArticleParticipantsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 活动的参与人关系表Service接口实现
 *
 * @author 510
 * @version 1.0.0 2021-04-23
 */
@Service
@Transactional(readOnly = true)
public class ArticleArticleParticipantsServiceImpl extends ServiceImpl<ArticleArticleParticipantsMapper, ArticleArticleParticipants> implements ArticleArticleParticipantsService {
    private static final Logger logger = LoggerFactory.getLogger(ArticleArticleParticipantsService.class);

    @Autowired
    private ArticleArticleParticipantsMapper articleArticleParticipantsMapper;

    /**
     *  分页查询列表
     */
    @Override
    public IPage<ArticleArticleParticipants> findArticleArticleParticipantsByCondition(ArticleArticleParticipantsCondition condition) {
        IPage<ArticleArticleParticipants> page = condition.buildPage();
        QueryWrapper<ArticleArticleParticipants> queryWrapper = condition.buildQueryWrapper(ArticleArticleParticipants.class);
        return this.page(page, queryWrapper);
    }

    /**
     *  查询活动的参与人
     */
    @ThreadPoolStatus("findParticipants--------------")
    @Async("asyncServer")
    public Future<List<ArticleArticleParticipants>> findParticipants(Integer aId){
        QueryWrapper<ArticleArticleParticipants> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("aid", "userId", "userName", "userAvatarUrl")
                .eq("aid", aId)
                .eq("deleteFlag", 1);
        return AsyncResult.forValue(this.list(queryWrapper));
    }

    /**
     *  查询活动的参与人数
     */
    @ThreadPoolStatus("t1--------------")
    @Async("asyncServer")
    public Future<Integer> findParticipantsNum(Integer aId){
        QueryWrapper<ArticleArticleParticipants> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("aid", "userId", "userName", "userAvatarUrl", "time")
                .eq("aid", aId)
                .eq("deleteFlag", 1);
        return AsyncResult.forValue(this.count(queryWrapper));
    }

    /**
     *  检验是否参与了项目 此处改为前端根据返回的详情列表判断
     */
    public Boolean CheckAddArticleCheck(Integer aId, Integer userId){

        return true;
    }

    /**
     *  参与活动
     */
    @Transactional
    public boolean addArticleArticleParticipants(Integer aid, String userId, String userName, String userAvatarUrl) {
        QueryWrapper<ArticleArticleParticipants> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid", aid)
                .eq("userId", userId);
        ArticleArticleParticipants articleArticleParticipants = this.getOne(queryWrapper);
        if (articleArticleParticipants != null){
            if (articleArticleParticipants.getDeleteFlag() == 0){
                UpdateWrapper<ArticleArticleParticipants> updateWrapper = new UpdateWrapper<>();
                updateWrapper
                        .eq("aid", aid)
                        .eq("userId", userId)
                        .set("deleteFlag", 1);
                articleArticleParticipantsMapper.update(null, updateWrapper);
                return true;
            }else return articleArticleParticipants.getDeleteFlag() == 1;
        }
        ArticleArticleParticipants articleArticleParticipants2 = new ArticleArticleParticipants();
        articleArticleParticipants2.setAid(aid);
        articleArticleParticipants2.setUserId(userId);
        articleArticleParticipants2.setUserName(userName);
        articleArticleParticipants2.setUserAvatarUrl(userAvatarUrl);
        articleArticleParticipants2.setTime(new Date());
        articleArticleParticipants2.setCreationDate(new Date());
        articleArticleParticipants2.setLastUpdateDate(new Date());
        articleArticleParticipants2.setCreatedBy("administrator");
        articleArticleParticipants2.setLastUpdatedBy("administrator");
        articleArticleParticipants2.setDeleteFlag(1);
        return this.save(articleArticleParticipants2);
    }

    /**
     *  取消参与活动
     */
    @Transactional
    public Boolean reArticleArticleParticipants(Integer aId, String userId){
        UpdateWrapper<ArticleArticleParticipants> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .eq("aId", aId)
                .eq("userId", userId)
                .set("deleteFlag", 0);

        return articleArticleParticipantsMapper.update(null, updateWrapper) == 1;
    }


    @Override
    public ArticleArticleParticipants getArticleArticleParticipantsById(Integer id) {
        return this.getById(id);
    }

    @Transactional
    @Override
    public boolean addArticleArticleParticipants(ArticleArticleParticipants articleArticleParticipants) {
        return this.save(articleArticleParticipants);
    }

    @Transactional
    @Override
    public boolean updateArticleArticleParticipants(ArticleArticleParticipants articleArticleParticipants) {
        return this.updateById(articleArticleParticipants);
    }

    @Transactional
    @Override
    public boolean deleteArticleArticleParticipantsById(Integer id) {
        return this.removeById(id);
    }

    @Transactional
    @Override
    public boolean deleteArticleArticleParticipantsByIds(List<Integer> idList) {
        return this.removeByIds(idList);
    }
}